package com.longkier.gx_library_springboot.Controller;

import com.longkier.gx_library_springboot.Service.HomeService;
import com.longkier.gx_library_springboot.entity.HomeRole;
import com.longkier.gx_library_springboot.repository.NoticeRepository;
import com.longkier.gx_library_springboot.utils.RequestUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/home")
@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
public class HomeController {

    private final NoticeRepository noticeRepository;
    private final HomeService homeService; // 注入 HomeService 实例

    // 获取指定数量的最新书籍
    @GetMapping("/newBook")
    public Map<String, Object> getLatestBooks() {
        try {
            return RequestUtils.buildResponse(homeService.homeGetLatestBooks(), 200);
        } catch (Exception e) {
            return RequestUtils.buildResponse("获取最新书籍发生异常", 500);
        }
    }

    // 获取指定数量的随机书籍
    @GetMapping("/randomBook")
    public Map<String, Object> getRandomBooks() {
        try {
            return RequestUtils.buildResponse(homeService.homeGetRandomBooks(), 200);
        } catch (Exception e) {
            return RequestUtils.buildResponse("获取随机推荐书籍发生异常", 500);
        }
    }

    // 首页获取最新1条公告
    @GetMapping("/notice")
    public Map<String, Object> getNotice() {
        try {
            Pageable pageable = PageRequest.of(0, 1);
            return RequestUtils.buildResponse(noticeRepository
                    .findAllByOrderByCreateTimeDesc(pageable), 200);
        } catch (Exception e) {
            return RequestUtils.buildResponse("获取公告信息时发生异常", 500);
        }
    }

    // 获取首页管理规则
    @GetMapping("/role")
    public Map<String, Object> getHomeRole() {
        try {
            return RequestUtils.buildResponse(homeService.getHomeRole(1L), 200);
        } catch (Exception e) {
            return RequestUtils.buildResponse("获取首页管理规则时发生异常", 500);
        }
    }

    // 更新首页管理规则
    @PutMapping("/role")
    public Map<String, Object> saveOrUpdateHomeRole(@RequestBody HomeRole homeRole) {
        try {
            return RequestUtils.buildResponse(homeService.saveOrUpdateHomeRole(homeRole), 200);
        } catch (Exception e) {
            return RequestUtils.buildResponse("保存或更新首页管理规则时发生异常", 500);
        }
    }

    // 上传首页轮播图
    @PutMapping("/uploadCarousel")
    public Map<String, Object> uploadCarouselImage(@RequestParam("file") MultipartFile file, @RequestParam("carouselIndex") int carouselIndex) {
        try {
            if (carouselIndex < 1 || carouselIndex > 3) {
                return RequestUtils.buildResponse("无效的轮播图索引", 400);
            }
            homeService.saveCarouselImage(file, carouselIndex, 1L);
            return RequestUtils.buildResponse("轮播图上传成功", 200);
        } catch (Exception e) {
            return RequestUtils.buildResponse(e.getMessage(), 500);
        }
    }

}
